package com.site.action.backend.dining;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import com.site.dto.dining.NewsAdvisoryBizBean;
import com.site.dto.dining.NewsSortBizBean;
import com.site.service.dining.NewsAdvisoryService;
import com.site.service.dining.NewsSortService;
import com.site.util.StringUtils;
import com.site.util.base.action.BaseAction;
import com.site.util.base.service.BaseService;

public class NewsAdvisoryAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private List<NewsSortBizBean> sortlist;
	private NewsSortBizBean sortbean;
	private NewsSortService sortservice;

	private List<NewsAdvisoryBizBean> newslist;
	private NewsAdvisoryBizBean newsbean;
	private NewsAdvisoryService newsservice;

	public List<NewsSortBizBean> getSortlist() {
		return sortlist;
	}

	public void setSortlist(List<NewsSortBizBean> sortlist) {
		this.sortlist = sortlist;
	}

	public NewsSortBizBean getSortbean() {
		return sortbean;
	}

	public void setSortbean(NewsSortBizBean sortbean) {
		this.sortbean = sortbean;
	}

	public NewsSortService getSortservice() {
		return sortservice;
	}

	@Resource
	public void setSortservice(NewsSortService sortservice) {
		this.sortservice = sortservice;
	}

	public List<NewsAdvisoryBizBean> getNewslist() {
		return newslist;
	}

	public void setNewslist(List<NewsAdvisoryBizBean> newslist) {
		this.newslist = newslist;
	}

	public NewsAdvisoryBizBean getNewsbean() {
		return newsbean;
	}

	public void setNewsbean(NewsAdvisoryBizBean newsbean) {
		this.newsbean = newsbean;
	}

	public NewsAdvisoryService getNewsservice() {
		return newsservice;
	}

	@Resource
	public void setNewsservice(NewsAdvisoryService newsservice) {
		this.newsservice = newsservice;
	}

	public String getNews() {
		if (queryPage == null) {
			initPagination();
		}
		if (sortlist == null) {
			sortlist = sortservice.getNewsSorts();
		}
		if (newsbean == null) {
			newsbean = new NewsAdvisoryBizBean();
		}
		if (sortbean != null) {
			newsbean.setType(sortbean.getId());
		}
		newslist = newsservice.getNewsAdvisories(queryPage, newsbean);
		return "getNews";
	}

	public String getNewsEdit() {
		if (sortlist == null) {
			sortlist = sortservice.getNewsSorts();
		}
		if (newslist != null && !newslist.isEmpty()) {
			for (NewsAdvisoryBizBean item : newslist) {
				if (null != item && item.getId() != null) {
					newsbean = newsservice.getEntityById(item.getId());
					sortbean.setId(newsbean.getType());
				}
			}
		}
		return "getNewsEdit";
	}

	public String getNewsAdd() {
		if (sortlist == null) {
			sortlist = sortservice.getNewsSorts();
		}
		return "getNewsAdd";
	}

	public String newsEdit() {
		if (CheckData(newsbean)) {
			newsbean.setType(sortbean.getId());
			//newsbean.setUpdatedUser(currentUser.getUserName());
			newsservice.execute(newsbean, BaseService.COMMAND_UPDATE);
			super.setMessageID("SYS_MSG_00022");
		} else {
			return getNewsEdit();
		}
		super.setBackurl(request.getContextPath()
				+ "/news/newsAction!getNews.do");
		return "toMessage";
	}

	public String newsAdd() {
		if (CheckData(newsbean)) {
			newsbean.setType(sortbean.getId());
			newsbean.setCreatedDate(new Date());
			//newsbean.setCreatedUser(currentUser.getUserName());
			//newsbean.setUpdatedUser(currentUser.getUserName());
			newsservice.execute(newsbean, BaseService.COMMAND_ADD);
			super.setMessageID("SYS_MSG_00022");
		} else {
			return getNewsAdd();
		}
		super.setBackurl(request.getContextPath()
				+ "/news/newsAction!getNews.do");
		return "toMessage";
	}

	public String newsDelete() {
		if (newslist != null && !newslist.isEmpty()) {
			for (NewsAdvisoryBizBean item : newslist) {
				if (null != item && item.getId() != null) {
					newsbean.setId(item.getId());
					newsservice.execute(newsbean, BaseService.COMMAND_DELETE);
				}
			}
		}
		super.setMessageID("SYS_MSG_00022");
		super.setBackurl(request.getContextPath()
				+ "/news/newsAction!getNews.do");
		return "toMessage";
	}

	private boolean CheckData(NewsAdvisoryBizBean newsbean2) {
		boolean mark = true;
		if (StringUtils.isEmpty(newsbean.getTitle())) {
			addFieldError("title", "新闻标题不能为空！");
			mark = false;
		}
		if (StringUtils.isEmpty(newsbean.getContent())) {
			addFieldError("content", "新闻内容不能为空！");
			mark = false;
		}
		return mark;

	}
}
